package com.ctshk.sapp.order.air.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.air.dto.AirOrderInfoDTO;
import com.ctshk.rpc.order.air.dto.AirOrderInvoiceInfoDTO;
import com.ctshk.rpc.order.air.dto.AirOrderListDTO;
import com.ctshk.rpc.order.air.dto.AirOrderReceiptInfoDTO;
import com.ctshk.rpc.order.air.req.*;
import com.ctshk.rpc.order.air.req.orderDetail.AirOrderUpdateTouristData;
import com.ctshk.rpc.order.air.service.IAirOrderInvoiceService;
import com.ctshk.rpc.order.air.service.IAirOrderReceiptService;
import com.ctshk.rpc.order.air.service.IAirOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 机票订单 前端控制器
 * </p>
 *
 * @author lihang
 * @since 2021-03-15
 */
@Api(value = "机票订单", tags = "机票订单")
@RestController
@RequestMapping("/air-order")
public class AirOrderController extends BaseController {


    @DubboReference
    private IAirOrderService iAirOrderService;

    @DubboReference
    private IAirOrderReceiptService iAirOrderReceiptService;

    @DubboReference
    private IAirOrderInvoiceService iAirOrderInvoiceService;

    @ApiOperation(value = "新增机票订单", notes = "新增机票订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "新增机票订单", operDesc = "新增机票订单")
    @PostMapping(value = "/add-order")
    public Result addOrder(@RequestBody @Valid AirOrderAddReq req) {
        return iAirOrderService.addOrder(req, getCurrentUser(getRequest()));
    }


    @ApiOperation(value = "新增机票外挂订单", notes = "新增机票外挂订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "新增机票外挂订单", operDesc = "新增机票外挂订单")
    @PostMapping(value = "/add-manual-order")
    public Result addManualOrder(@RequestBody @Valid AirManualOrderAddReq req) {
        return iAirOrderService.addManualOrder(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "支付订单/订单收款", notes = "支付订单/订单收款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "支付订单/订单收款", operDesc = "支付订单/订单收款")
    @PostMapping(value = "/pay-order")
    public Result payOrder(@RequestBody @Valid AirOrderPayReq req) {
        return iAirOrderService.payOrder(req, getCurrentUser(getRequest()));
    }


    @ApiOperation(value = "取消订单/订单退款", notes = "取消订单/订单退款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "取消订单/订单退款", operDesc = "取消订单/订单退款")
    @PostMapping(value = "/refund-order")
    public Result refundOrder(@RequestBody @Valid AirOrderRefundReq req) {
        return iAirOrderService.refundOrder(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "未付款订单直接取消订单", notes = "未付款订单直接取消订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "未付款订单直接取消订单", operDesc = "未付款订单直接取消订单")
    @PostMapping(value = "/cancel-order")
    public Result cancelOrder(@RequestBody @Valid AirOrderCancelReq req) {
        return iAirOrderService.cancelOrder(req, getCurrentUser(getRequest()));
    }

    /**
     * 退款审批
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "退款审批", notes = "退款审批")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "退款审批", operDesc = "退款审批")
    @PostMapping(value = "refundApproval")
    public Result refundApproval(@RequestBody AirOrderRefundApprovalReq req) {
        return iAirOrderService.refundApproval(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "订单退款确认", notes = "订单退款确认")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "订单退款确认", operDesc = "订单退款确认")
    @PostMapping(value = "/refund-confirm/{id}")
    public Result refundConfirm(@PathVariable(required = true, name = "id") @NotNull Long id) {
        return iAirOrderService.refundConfirm(id, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "机票订单列表", notes = "机票订单列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机票订单列表", operDesc = "机票订单列表")
    @PostMapping(value = "/list")
    public Result<PageResponse<AirOrderListDTO>> list(@RequestBody AirOrderListReq req) {
        return iAirOrderService.list(req);
    }

    @ApiOperation(value = "机票订单详情", notes = "机票订单详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机票订单详情", operDesc = "机票订单详情")
    @PostMapping(value = "/info/{id}")
    public Result<AirOrderInfoDTO> info(@PathVariable Long id) {
        return iAirOrderService.info(id);
    }


    @ApiOperation(value = "机票订单收據详情", notes = "机票订单收據详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机票订单收據详情", operDesc = "机票订单收據详情")
    @PostMapping(value = "/info/receipt/{id}")
    public Result<AirOrderReceiptInfoDTO> infoReceipt(@PathVariable Long id) {
        return iAirOrderReceiptService.info(id);
    }

    @ApiOperation(value = "編輯机票订单收據", notes = "編輯机票订单收據")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "編輯机票订单收據", operDesc = "編輯机票订单收據")
    @PostMapping(value = "/edit/receipt")
    public Result<AirOrderReceiptInfoDTO> editReceipt(@RequestBody AirOrderReceiptEditReq req) {
        return iAirOrderReceiptService.edit(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "新增订单发票", notes = "新增订单发票")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "新增订单发票", operDesc = "新增订单发票")
    @PostMapping(value = "/add/invoice")
    public Result<AirOrderInvoiceInfoDTO> addInvoice(Long orderId) {
        return iAirOrderInvoiceService.add(orderId, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "订单发票详情", notes = "订单发票详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单发票详情", operDesc = "订单发票详情")
    @PostMapping(value = "/info/invoice")
    public Result<AirOrderInvoiceInfoDTO> infoInvoice(Long orderId) {
        return iAirOrderInvoiceService.info(orderId);
    }


    @ApiOperation(value = "订单发票编辑", notes = "订单发票编辑")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "订单发票编辑", operDesc = "订单发票编辑")
    @PostMapping(value = "/edit/invoice")
    public Result editInvoice(@RequestBody AirOrderInvoiceEditReq req) {
        return iAirOrderInvoiceService.edit(req, getCurrentUser(getRequest()));
    }


    /**
     * 更新旅客信息
     *
     * @param airOrderUpdateTouristData
     * @return
     */
    @ApiOperation(value = "旅客信息编辑", notes = "旅客信息编辑")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "旅客信息编辑", operDesc = "旅客信息编辑")
    @PutMapping("/edit/updateTouristInfo")
    public Result updateTouristInfo(@RequestBody @Valid AirOrderUpdateTouristData airOrderUpdateTouristData) {
        return iAirOrderService.updateTouristInfo(airOrderUpdateTouristData);
    }

    /**
     * 通知旅客订单确认成功
     *
     * @param sendMessageToTouristReq
     * @return
     */
    @ApiOperation(value = "通知旅客订单确认成功", notes = "通知旅客订单确认成功")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "通知旅客订单确认成功", operDesc = "通知旅客订单确认成功")
    @PostMapping("/sendMessageToTourist")
    public Result sendMessageToTourist(@RequestBody @Valid AirOrderSendMessageToTouristReq sendMessageToTouristReq) {
        return iAirOrderService.sendMessageToTourist(sendMessageToTouristReq);
    }

    /**
     * 确认订单
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "确认订单", notes = "确认订单")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "确认订单", operDesc = "确认订单")
    @PostMapping("/confirmOrder")
    public Result confirmOrder(@RequestBody @Valid AirOrderConfirmOrderReq req) {
        return iAirOrderService.confirmOrder(req);
    }

    /**
     * 再次确认收款
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "再次确认收款", notes = "再次确认收款")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "再次确认收款", operDesc = "再次确认收款")
    @PostMapping("/reconfirmThePayment")
    public Result reconfirmThePayment(@RequestBody @Valid ReconfirmThePaymentReq req) {
        return iAirOrderService.reconfirmThePayment(req, getCurrentUser(getRequest()));
    }


//    @ApiOperation(value = "編輯机票订单收據", notes = "編輯机票订单收據")
//    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "編輯机票订单收據", operDesc = "編輯机票订单收據")
//    @PostMapping(value="/edit/invoice")
//    public Result<AirOrderReceiptInfoDTO> editInvoice(@RequestBody AirOrderReceiptEditReq req){
//        return  iAirOrderReceiptService.edit(req,getCurrentUser(getRequest()));
//    }

    /**
     * 锁定产品库存(内部接口)
     *
     * @param req
     * @param
     * @return
     */
    @PostMapping("/lockInventory")
    public Result lockInventory(@RequestBody LockInventoryReq req) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setId(110L);
        tokenUser.setUsername("admin");
        return iAirOrderService.lockInventory(req, tokenUser);
    }

    /**
     * 释放库存
     *
     * @param productId
     * @param orderId
     * @return
     */
    @RequestMapping("/releaseInventory")
    public Result releaseInventory(@RequestParam Long productId, @RequestParam Long orderId) {
        return iAirOrderService.releaseInventory(productId, orderId);
    }


}
